﻿using CarPCBATest.Procedure;
using CarPCBATest.ViewModel;
using MahApps.Metro.Controls;
using MoreLinq;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace CarPCBATest.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class HomeView : UserControl
    {
        public HomeView()
        {
            InitializeComponent();
            var model = new HomeViewModel();
            this.DataContext = model;
            model.control = this.VmRenderControl;

            model.SNInputFocusSwitchAction = new System.Action<int>((index) =>
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    TextBox focusBox = FindName("tbSN" + index.ToString()) as TextBox;
                    if (focusBox != null)
                    {
                        focusBox.Focus();
                        focusBox.SelectAll();
                    }
                });
            });
            //model.SNInputVisibilityAction = new System.Action<int>((count) =>
            //{
            //    int stationCount = 10;
            //    Application.Current.Dispatcher.Invoke(() =>
            //    {
            //        if (count < 1 || count > stationCount)
            //            return;
            //        for (int i = 0; i < stationCount; i++)
            //        {
            //            MetroHeader currBox = FindName("mhSN" + i.ToString()) as MetroHeader;
            //            if (currBox != null)
            //            {
            //                if (i < count)
            //                {
            //                    currBox.Visibility = Visibility.Visible;
            //                }
            //                else
            //                {
            //                    currBox.Visibility = Visibility.Collapsed;
            //                }
            //            }
            //        }
            //    });
            //});

            model.ConfigChangedAction = new System.Action<PcbConfig>((config) => {
                if (config != null)
                {
                    
                    Application.Current.Dispatcher.Invoke(() => {


                        var stations = config.ListSingBoardConfig.DistinctBy(x => x.StationNo).ToList();


                        for (int i = 0; i < stations.Count(); i++)
                        {
                            MetroHeader currBox = FindName("mhSN" + (i).ToString()) as MetroHeader;
                            if (currBox != null)
                            {
                                currBox.Header = $"工位{ stations[i].StationNo}:";
                            }
                        }

                        int stationCount = 10;
                        for (int i = 0; i < stationCount; i++)
                        {
                            MetroHeader currBox = FindName("mhSN" + i.ToString()) as MetroHeader;
                            if (currBox != null)
                            {
                                if (i < stations.Count())
                                {
                                    currBox.Visibility = Visibility.Visible;
                                }
                                else
                                {
                                    currBox.Visibility = Visibility.Collapsed;
                                }
                            }
                        }
                    });
                }
            
            });
        }
    }
}